package cn.livingCloud.controller.content.validator;

import cn.livingCloud.dto.TextReplyContentDto;
import cn.livingCloud.dto.TextReplyRuleFormDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by 唐旭 on 2015/12/26.
 */
@Component
public class TextReplyRuleFormDtoValidator extends AbstractRuleDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return TextReplyRuleFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        TextReplyRuleFormDto formDto = (TextReplyRuleFormDto) target;
        validateRule(formDto, errors);
        validateContent(formDto.getContent(), errors);
    }

    private void validateContent(TextReplyContentDto content, Errors errors) {
        if (StringUtils.isEmpty(content.getContent())) {
            errors.rejectValue("content.content", null, "请填写回复内容");
        }
    }
}
